package com.hrbxlh.util;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesUtil {

	static Properties props = new Properties();

	// 接口文件
	public String getInterfaceUrl(String interfaceName) {

		System.out.println("出来！！！！");

		// Properties prop = new Properties();
		String backValue = "";
		try {
			// 读取属性文件WXInterfaceUrl.properties
			// InputStream in = new BufferedInputStream(new FileInputStream(
			// "wxControl/resources/WXInterfaceUrl.properties"));

			String filename = "UtilFile.properties";
			Properties props = new Properties();
			props.load(PropertiesUtil.class.getClassLoader()
					.getResourceAsStream(filename));
			// prop.load(in); // /加载属性列表
			System.out.println("-------------------");
			Iterator<String> it = props.stringPropertyNames().iterator();
			while (it.hasNext()) {
				System.out.println("????????????");
				String key = it.next();
				if (interfaceName.equals(key)) {
					backValue = props.getProperty(key);
					break;
				}
			}
			// in.close();

		} catch (Exception e) {
			backValue = "";
			e.printStackTrace();
		}
		return backValue;
	}

	// 接口文件
	public static String[] getUrl(String[] interfaceNameArr) {
		Map<String, String> returnMap = new HashMap<String, String>();
		String[] valueArr = new String[interfaceNameArr.length];
		try {
			String filename = "UtilFile.properties";
			Properties props = new Properties();
			props.load(PropertiesUtil.class.getClassLoader()
					.getResourceAsStream(filename));
			Iterator<String> it = props.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				for (String interfaceName : interfaceNameArr) {
					if (interfaceName.equals(key)) {
						returnMap.put(key, props.getProperty(key));
					}
				}
			}
			int index = 0;

			for (String interfaceName : interfaceNameArr) {
				for (Map.Entry<String, String> entry : returnMap.entrySet()) {
					System.out.println(interfaceNameArr[index] + " -- "
							+ entry.getKey());
					if (interfaceName.equals(entry.getKey())) {
						valueArr[index] = entry.getValue();
						index++;
						break;
					}
					if (index == interfaceNameArr.length) {
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueArr;
	}

	// 财务流程配置文件
	public String getFinanceProInfo(String propertiesName) {

		String backValue = "";
		try {
			// 读取属性文件FinanceProUtil.properties
			String filename = "FinanceProUtil.properties";
			Properties props = new Properties();
			props.load(PropertiesUtil.class.getClassLoader()
					.getResourceAsStream(filename));
			Iterator<String> it = props.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				if (propertiesName.equals(key)) {
					backValue = props.getProperty(key);
					break;
				}
			}

		} catch (Exception e) {
			backValue = "";
			e.printStackTrace();
		}
		return backValue;

	}

	// 一套公有的，以后都调用这个方法就可以了。
	/**
	 * 获取文件信息
	 * 
	 * @param proFileName
	 *            文件名称
	 * @param Map
	 *            <String, Object> paramMap 参数集合 
	 * @return
	 */
	public static Map<String, Object> getProUtil(String proFileName,
			Map<String, Object> paramMap) {
		
		Object proValue = null;

		try {

			if (paramMap != null) {

				// 加载配置文件
				props.load(PropertiesUtil.class.getClassLoader()
						.getResourceAsStream(proFileName));
				
				// 将配置文件内容传到hashmap
				Map<String, String> proMap = new HashMap<String, String>((Map) props);
				
				// 将参数赋值
				for(Map.Entry<String, Object> paramEntry : paramMap.entrySet())
		            proValue = proMap.get(paramEntry.getKey()) == null ? "" : paramMap.put(paramEntry.getKey(), proMap.get(paramEntry.getKey()).toString());
						
			}

		} catch (Exception e) {
			paramMap = null;
			e.printStackTrace();
		}

		return paramMap;

	}
	
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("AttendanceInfo", "");
		map.put("project_name", "");
		map.put("project_name1", "");
		System.out.println(PropertiesUtil.getProUtil("UtilFile.properties", map));
	}

	/*
	 * public static void main(String[] args) throws Exception {
	 * 
	 * Map<String, Object> map = new HashMap<String, Object>();
	 * map.put("AttendanceInfo", "");
	 * 
	 * PropertiesUtil.writeProperties("AttendanceInfo",
	 * "[{\"dep_id\":23,\"sign_time\":\"08:30:00\",\"sign_out_time\":\"17:30:00\"},{\"dep_id\":9,\"sign_time\":\"08:30:00\",\"sign_out_time\":\"17:00:00\"},{\"dep_id\":18,\"sign_time\":\"08:30:00\",\"sign_out_time\":\"17:00:00\"},{\"dep_id\":19,\"sign_time\":\"09:00:00\",\"sign_out_time\":\"18:00:00\"}]"
	 * ); System.out.println("======================");
	 * 
	 * Properties prop = new Properties();
	 * 
	 * prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(
	 * "UtilFile.properties"));
	 * 
	 * Map<String, String> hMap = new HashMap<String, String>((Map) prop);
	 * 
	 * System.out.println(hMap);
	 * 
	 * map.put("signatureImg", "");
	 * 
	 * // map.put("signatureImg", "/upload/signatureImg/");
	 * 
	 * // hMap.entrySet().retainAll(map.entrySet()); // //
	 * System.out.println(hMap);
	 * 
	 * }
	 */

	/*public static void main(String[] args) {
		
		Map<String, Object> keyValueMap = new HashMap<String, Object>();
		keyValueMap.put("AttendanceInfo", "123");
		 String fileName = "UtilFile.properties";
		 String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();  
	        Properties props = null;  
	        BufferedWriter bw = null;  
	  
	        try {  
	        	
	        	System.out.println(new PropertiesUtil().getClass().getResource("/").getPath());
	        	
	            filePath = URLDecoder.decode(filePath,"utf-8");      
	            
	            props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));  
	            
	            // 写入属性文件  
	            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));  
	              
	            props.clear();// 清空旧的文件  
	              
	            for (String key : keyValueMap.keySet())  {
	                props.setProperty(key, (String) keyValueMap.get(key));  
	            }
	           System.out.println(props);
	            props.store(bw, "");  
		} catch (Exception e) {
			System.out.println(e);
		}
	}*/
}
